package com.coderman.infosys.auth.infrast.repositoryimpl;

import com.coderman.infosys.auth.domain.bo.authority.AdminAuthorityBO;
import com.coderman.infosys.auth.domain.bo.authority.AuthorityBO;
import com.coderman.infosys.auth.domain.bo.authority.DataAuthorityBO;
import com.coderman.infosys.auth.domain.bo.authority.SystemAuthorityBO;
import com.coderman.infosys.auth.domain.enums.errors.AuthorityErrorEnum;
import com.coderman.infosys.auth.domain.repository.AuthorityRepository;
import com.coderman.infosys.auth.infrast.dao.mapper.AuthorityMapper;
import com.coderman.infosys.auth.infrast.dao.mapper.SystemAuthorityMapper;

import com.coderman.infosys.auth.infrast.dataconvert.AdminAuthorityConvert;
import com.coderman.infosys.auth.infrast.dataconvert.AuthorityConvert;
import com.coderman.infosys.auth.infrast.dao.mapper.AdminAuthorityMapper;
import com.coderman.infosys.auth.infrast.dao.mapper.DataAuthorityMapper;
import com.coderman.infosys.auth.infrast.dao.dataobject.AuthorityDO;
import com.coderman.infosys.auth.infrast.dataconvert.DataAuthorityConvert;
import com.coderman.infosys.auth.infrast.dataconvert.SystemAuthorityConvert;
import com.coderman.infosys.plat.auth.common.exception.ExceptionUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Description:接口实现类
 * @Author：
 * @CreateTime：2022-01-02 15:27:38
 * @version v1.0
 */
@Service
public class AuthorityRepositoryImpl  implements AuthorityRepository {

	private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired
    private SystemAuthorityMapper systemAuthorityMapper;

    @Autowired
    private AdminAuthorityMapper adminAuthorityMapper;

    @Autowired
    private DataAuthorityMapper dataAuthorityMapper;

    //@Autowired
    //private DepartmentServiceAdapter departmentServiceAdapter;


    @Override
	public Boolean authToRole(AuthorityBO authorityBO){
        AuthorityDO authorityDO = AuthorityConvert.INSTANCE.bo2do(authorityBO);
        authorityDO.setAuthorityTypeStr(StringUtils.join(authorityBO.getAuthorityTypeSet(),","));

        authorityMapper.insert(authorityDO);

        if(authorityBO.withDataAuth()){
            for (DataAuthorityBO dataAuthorityBO : authorityBO.getDataAuthorityBOList()){
                dataAuthorityMapper.insert(DataAuthorityConvert.INSTANCE.bo2do(dataAuthorityBO));
            }
        }

        if(authorityBO.withAdminSystemAuth()){
            for (AdminAuthorityBO adminAuthorityBO : authorityBO.getAdminAuthorityBOList()){
                adminAuthorityMapper.insert(AdminAuthorityConvert.INSTANCE.bo2do(adminAuthorityBO));
            }
        }

        if(authorityBO.withSystemAuth()){
            for (SystemAuthorityBO systemAuthorityBO : authorityBO.getSystemAuthorityBOList()){
                systemAuthorityMapper.insert(SystemAuthorityConvert.INSTANCE.bo2do(systemAuthorityBO));
            }
        }

        return true;
    }


    @Override
	public Boolean updateAuthToRole(AuthorityBO authorityBO){
        //走删除再添加的策略
        adminAuthorityMapper.deleteByAuthCode(authorityBO.getAuthCode());
        systemAuthorityMapper.deleteByAuthCode(authorityBO.getAuthCode());
        dataAuthorityMapper.deleteByAuthCode(authorityBO.getAuthCode());
        //todo 更新AuthorityBO.authorityTypeSet
        if(CollectionUtils.isNotEmpty(authorityBO.getDataAuthorityBOList())){
            for (DataAuthorityBO dataAuthorityBO : authorityBO.getDataAuthorityBOList()){
                dataAuthorityMapper.insert(DataAuthorityConvert.INSTANCE.bo2do(dataAuthorityBO));
            }
        }

        if(CollectionUtils.isNotEmpty(authorityBO.getAdminAuthorityBOList())){
            for (AdminAuthorityBO adminAuthorityBO : authorityBO.getAdminAuthorityBOList()){
                adminAuthorityMapper.insert(AdminAuthorityConvert.INSTANCE.bo2do(adminAuthorityBO));
            }
        }

        if(CollectionUtils.isNotEmpty(authorityBO.getSystemAuthorityBOList())){
            for (SystemAuthorityBO systemAuthorityBO : authorityBO.getSystemAuthorityBOList()){
                systemAuthorityMapper.insert(SystemAuthorityConvert.INSTANCE.bo2do(systemAuthorityBO));
            }
        }
        return true;
    }

    @Override
	public Boolean disableAuthority(String authorityCode){
        AuthorityDO authorityDO = authorityMapper.getByCode(authorityCode);
        AuthorityBO authorityBO = AuthorityConvert.INSTANCE.do2bo(authorityDO);
        if(authorityBO == null){
            throw ExceptionUtils.error(AuthorityErrorEnum.AUTHORITY_NOF_FUND_ERROR);
        }
        authorityBO.disable();
        int count = authorityMapper.update(AuthorityConvert.INSTANCE.bo2do(authorityBO));
        return count == 1;
    }

    @Override
	public AuthorityBO getByCode(String authorityCode){
        AuthorityDO authorityDO = authorityMapper.getByCode(authorityCode);
        return AuthorityConvert.INSTANCE.do2bo(authorityDO);
    }

    @Override
	public AuthorityBO getByRoleCode(String roleGroupCode){
        AuthorityDO authorityDO = authorityMapper.getByRoleGroupCode(roleGroupCode);
        return AuthorityConvert.INSTANCE.do2bo(authorityDO);
    }

    @Override
    public AuthorityBO getByRoleGroupCode(String roleGroupCode){
        AuthorityDO authorityDO = authorityMapper.getByRoleGroupCode(roleGroupCode);
        return AuthorityConvert.INSTANCE.do2bo(authorityDO);
    }
}